package com.example.basemodel.base

import android.os.Bundle
import android.view.Gravity
import android.view.WindowManager
import androidx.annotation.ColorInt
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import com.kt.NetworkModel.utils.LoadingNetUtils
import com.kt.NetworkModel.utils.ToastUtils
import java.lang.reflect.ParameterizedType

abstract class BaseHeadVMActivity<VM : BaseViewModel, DB : ViewDataBinding, HS : BaseStyle> : AppCompatActivity() {
    lateinit var mViewModel: VM
    lateinit var mDatabind: DB
    lateinit var mHeadStyle: HS
    private var toast: ToastUtils? = null

    abstract fun layoutId(): Int
    abstract fun initView(savedInstanceState: Bundle?)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        toast = ToastUtils(this)
        initDataBind()
        init(savedInstanceState)
        initStatusBar()
        // 设置窗口为常亮状态
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
    }

    private fun init(savedInstanceState: Bundle?) {
        mViewModel = createViewModel()
        initView(savedInstanceState)
        createObserver()
    }

    fun setHeadTitle(title: String) {
        if (mHeadStyle is DefaultHeadStyle) {
            (mHeadStyle as DefaultHeadStyle).setHeadTitle(title)
        }
    }

    fun setHeadTitle(title: String, @ColorInt color: Int) {
        if (mHeadStyle is DefaultHeadStyle) {
            (mHeadStyle as DefaultHeadStyle).setTitleColor(title, color)
        }
    }


    /**
     * 创建viewModel
     */
    fun createViewModel(): VM {
        return ViewModelProvider(this).get(getVmClazz(this))
    }

    /**
     * 创建LiveData数据观察者
     */
    abstract fun createObserver()

    /**
     *  初始化Databinding操作
     */
    open fun initDataBind() {
        //获取头部View
        mDatabind = DataBindingUtil.inflate(layoutInflater, layoutId(), null, false)
        mHeadStyle = getHSVmClazz(this)
        setContentView(mHeadStyle.initHedView(this, mDatabind.root))
//      mDatabind = DataBindingUtil.setContentView(this, layoutId())
        mDatabind.lifecycleOwner = this
    }


    protected open fun initStatusBar() {

    }

    /**
     * 显示进度条
     */
    fun showLoading() {
        LoadingNetUtils.getInstance().showLoading(this)
    }

    /**
     * 关闭进度条
     */
    fun dismissLoading() {
        LoadingNetUtils.getInstance().dismissLoading()
    }

    /**
     * 自定义Toast文字
     */
    fun showMsg(msg: String) {
        toast?.InitToast()
        toast?.setText(msg)
        toast?.setGravity(Gravity.CENTER)
        toast?.show()
    }

    /**
     * 自定义Toast图片+文字
     */
    fun showMsgimage(msg: String, url: Int) {
        toast?.InitToast()
        toast?.setText(msg)
        toast?.setImage(url)
        toast?.setGravity(Gravity.CENTER)
        toast?.show()
    }

//    fun showMsg(str: String) {
//        Toaster.show(str)
//    }


    fun <HS> getHSVmClazz(obj: Any): HS {
//        for (item in (obj.javaClass.genericSuperclass as ParameterizedType).actualTypeArguments) {
//            MLog.d("---> $item")
//        }
        return ((obj.javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[2] as Class<HS>).newInstance()
    }

    /**
     * 获取当前类绑定的泛型ViewModel-clazz
     */
    @Suppress("UNCHECKED_CAST")
    fun <VM> getVmClazz(obj: Any): VM {
//    for (item in (obj.javaClass.genericSuperclass as ParameterizedType).actualTypeArguments) {
//        MLog.d("---> $item")
//    }
        return (obj.javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as VM
    }

}
